package com.chenyue.cm.msg.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.msg.domain.JdMsg;
import com.chenyue.cm.msg.domain.JdMsgExample;
import com.chenyue.cm.msg.service.JdMsgService;
import com.chenyue.cm.push.jpush.PushUtils;
import com.chenyue.cm.push.jpush.vo.NotifyType;
import com.chenyue.cm.push.jpush.vo.PushContent;
import com.chenyue.cm.push.jpush.vo.PushParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdMsg")
public class JdMsgController {

    private final JdMsgService jdMsgService;

    public JdMsgController(JdMsgService jdMsgService) {
        this.jdMsgService = jdMsgService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdMsg> result = jdMsgService.getPageBean(pageInfo, JdMsg.class);
        return Success.success(result);
    }

    @RequestMapping("/getJdMsgList")
    public Success getJdMsgList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<JdMsg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdMsg::getType, 1);
        wrapper.eq(JdMsg::getTransType, 2);
        Map<String, Object> pageSearch = pageInfo.getPageSearch();
        if (pageSearch != null) {
            if (null != pageSearch.get("SEARCH_LIKE_content")) {
                wrapper.like(JdMsg::getContent, String.valueOf(pageSearch.get("SEARCH_LIKE_content")));
            }
        }
        List<JdMsg> list = jdMsgService.list(wrapper);
        //封装返回结果
        PageInfo<JdMsg> result = new PageInfo<>(list);

        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdMsg vo) {
        vo.setCt(Calendar.getInstance().getTime());
        vo.setType(1);
        vo.setTransType(NotifyType.SYSTEM_NOTIFY.getCode());
        vo.setTransParam("");
        vo.setUserId(null);
        boolean success = jdMsgService.save(vo);
        PushContent content = new PushContent();
        content.setContent(vo.getContent());
        content.setParam("");
        PushParam param = new PushParam(null, content, NotifyType.SYSTEM_NOTIFY.getCode());
        PushUtils.push(param);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo 修改实体
     * @param request id
     *
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdMsg vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdMsgService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdMsgExample aExample = new JdMsgExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdMsgService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdMsgService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}
}
